package my.mybatis.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.ToString;
import my.mybatis.common.util.OperatorConventor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:wangbin.
 * @Description.
 * @Date:Created in 17:43 2017/9/15.
 * @Modified By:
 */
@ToString
@ApiModel(description = "特殊查询参数对象")
public class QueryParam implements Serializable{

    private static final long serialVersionUID = -7559248959784802612L;

    @ApiModelProperty(value = "查询属性")
    private String key;
    @ApiModelProperty(value = "查询值")
    private Object value;
    @ApiModelProperty(value = "查询符号（eq:'=',gt:'>',ge:'>=',lt:'<',le:'<=',like:'like',notLike:'not like',ne:'<>',isNull:'is null',isNotNull:'is not null',in:'in',notIn:'not in'）")
    private String operator = OperatorConventor.EQ;

    public static class Builder {
        private String key;
        private Object value;
        private String operator = OperatorConventor.EQ;

        public Builder() {
        }

        public Builder key(String key) {
            this.key = key;
            return this;
        }

        public Builder operator(String operator) {
            this.operator = operator;
            return this;
        }

        public Builder value(Object value) {
            this.value = value;
            return this;
        }

        public QueryParam build() {
            return new QueryParam(this);
        }
    }

    private QueryParam(Builder builder) {
        this.key = builder.key;
        this.operator = builder.operator;
        this.value = builder.value;
        // 检测operator类型
        checkOperationType(this.operator);
        // 检测value类型
        checkValueType(this.value);
    }

    private boolean noValue = false;
    private boolean singleValue = false;
    private boolean isLike = false;
    private boolean listValue = false;

    public boolean isNoValue() {
        return noValue;
    }

    public boolean isLike() {
        return isLike;
    }

    public boolean isSingleValue() {
        return singleValue;
    }

    public boolean isListValue() {
        return listValue;
    }

    public QueryParam() {
    }

    public String getOperator() {
        return operator;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(final Object value) {
        this.value = value;
        checkValueType(value);
    }

    public void setOperator(final String operator) {
        this.operator = operator;
        checkOperationType(operator);
    }

    public QueryParam(final String key,final Object value,final String operator) {
        this.key = key;
        this.value = value;
        this.operator = operator;
    }

    private void checkOperationType(final String operator) {
        if (OperatorConventor.ISNULL.equals(operator) || OperatorConventor.ISNOTNULL.equals(operator))
            this.noValue = true;
        if (OperatorConventor.LIKE.equals(operator))
            this.isLike = true;
        if (OperatorConventor.IN.equals(operator) || OperatorConventor.NOTIN.equals(operator))
            this.listValue = true;
    }

    /**数组转成list的类型**/
    public static final String ARRAYS_TO_ARRAYLIST = "java.util.Arrays$ArrayList";

    private void checkValueType(final Object value) {
        if (value.getClass() == List.class ||
                value.getClass() == ArrayList.class ||
                value.getClass().getName().equals(ARRAYS_TO_ARRAYLIST) ||
                value.getClass().getName().substring(0,2).equals("[L"))
            this.listValue = true;
        else
            this.singleValue = true;
    }
}
